define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'course_plan/index' + location.search,
                    add_url: 'course_plan/add',
                    edit_url: 'course_plan/edit',
                    del_url: 'course_plan/del',
                    multi_url: 'course_plan/multi',
                    import_url: 'course_plan/import',
                    table: 'course_plan',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'venue.name', title: __('所属场馆'),operate:false},
                        {field: 'venue_id', title: __('所属场馆'),addclass: 'selectpage', extend:'data-source="ajax/venue"', defaultValue:Config.vid, visible:false,},
                        {field: 'course_id', title: __('Course_id'),addclass: 'selectpage', extend:'data-source="ajax/course"', visible:false},
                        {field: 'course.name', title: __('课程名称'), operate: false},
                        {field: 'name', title: __('Name'), operate: 'LIKE',formatter:function(value,row,index){
                                url = "course_node/index?course_plan_id=" + row.id;
                                // url = "example/bootstraptable?" + this.field + "=" + value;

                                //方式一,直接返回class带有addtabsit的链接,这可以方便自定义显示内容
                                return '<a href="' + url + '" class="label label-success addtabsit" title="' + __("Search %s", value) + '">' + __('%s', value) + '</a>';
                                //return '<a href="' + url + '" class="label label-success addtabsit" title="' + __("Search %s", row.id) + '">' + __('Search %s', row.id) + '</a>';

                                //方式二,直接调用Table.api.formatter.addtabs
                                //this.url = url;
                                //return Table.api.formatter.addtabs.call(this, value, row, index);
                            }
                        },
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                       /* {field: 'ext', title: __('Ext'), operate: 'LIKE'},
                        {field: 'weigh', title: __('Weigh'), operate: false},*/
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.status},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'course_plan/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'course_plan/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'course_plan/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});